package com.chenfan.mcn.utils;

import com.chenfan.common.exception.BusinessException;
import com.chenfan.common.exception.ResultState;
import com.chenfan.common.exception.SystemState;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 业务断言
 *
 * @author wulg
 * @date 2021-07-22
 **/
public class BusinessAssert {

    public static void defaultThrow(ResultState resultState) {
        throw new BusinessException(resultState);
    }

    public static void isNull(Object object, ResultState resultState) {
        isNull(object, resultState, null);
    }

    public static void isNull(Object object, String message) {
        isNull(object, null, message);
    }

    public static void isNull(Object object, ResultState resultState, String message) {
        if (Objects.nonNull(object)) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message, resultState.message()));
        }
    }

    public static void notNull(Object object, ResultState resultState) {
        notNull(object, resultState, null);
    }

    public static void notNull(Object object, String message) {
        notNull(object, null, message);
    }

    public static void notNull(Object object,  ResultState resultState, String message) {
        if (Objects.isNull(object)) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void isTrue(boolean expression, ResultState resultState) {
        isTrue(expression, resultState, null);
    }

    public static void isTrue(boolean expression, String message) {
        isTrue(expression, null, message);
    }

    public static void isTrue(boolean expression, ResultState resultState, String message) {
        if (!expression) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void isFalse(boolean expression, String message) {
        isFalse(expression, null, message);
    }

    public static void isFalse(boolean expression, ResultState resultState) {
        isFalse(expression, resultState, null);
    }

    public static void isFalse(boolean expression, ResultState resultState, String message) {
        if (expression) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void notEmpty(String text, String message) {
        notEmpty(text, null, message);
    }

    public static void notEmpty(String text, ResultState resultState, String message) {
        if (StringUtils.isEmpty(text)) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void notBlank(String text, ResultState resultState) {
        notBlank(text, resultState, null);
    }

    public static void notBlank(String text, String message) {
        notBlank(text, null, message);
    }

    public static void notBlank(String text, ResultState resultState, String message) {
        if (StringUtils.isBlank(text)) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void notEmpty(Object[] array, ResultState resultState) {
        if (ObjectUtils.isEmpty(array)) {
            throw new BusinessException(resultState);
        }
    }

    public static void notEmpty(Object[] array, ResultState resultState, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void notEmpty(Collection<?> collection, String message) {
        notEmpty(collection, null, message);
    }

    public static void notEmpty(Collection<?> collection, ResultState resultState) {
        notEmpty(collection, resultState, null);
    }

    public static void notEmpty(Collection<?> collection, ResultState resultState, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            resultState = ObjectUtils.defaultIfNull(resultState, SystemState.BUSINESS_ERROR);
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void notEmpty(Map<?, ?> map, ResultState resultState) {
        if (MapUtils.isEmpty(map)) {
            throw new BusinessException(resultState);
        }
    }

    public static void notEmpty(Map<?, ?> map, ResultState resultState, String message) {
        if (MapUtils.isEmpty(map)) {
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(message,resultState.message()));
        }
    }

    public static void maxLength(String text, int maxLength, ResultState resultState) {
        if (StringUtils.isNotBlank(text) && text.length() > maxLength) {
            throw new BusinessException(resultState);
        }
    }

    public static void maxLength(String text, int maxLength, ResultState resultState, String message) {
        if (StringUtils.isNotBlank(text) && text.length() > maxLength) {
            String newMessage = null;
            if(StringUtils.isNotBlank(message)) {
                newMessage = String.format(message, maxLength);
            }
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(newMessage,resultState.message()));
        }
    }

    public static void maxScale(BigDecimal bd, int maxScale, ResultState resultState) {
        if (Objects.nonNull(bd) && bd.scale() > maxScale) {
            throw new BusinessException(resultState);
        }
    }

    public static void maxScale(BigDecimal bd, int maxScale, ResultState resultState, String message) {
        if (Objects.nonNull(bd) && bd.scale() > maxScale) {
            String newMessage = null;
            if(StringUtils.isNotBlank(message)) {
                newMessage = String.format(message, maxScale);
            }
            throw new BusinessException(resultState.code(), StringUtils.defaultIfBlank(newMessage,resultState.message()));
        }
    }
}
